//
//  WTEmoticonToolbar.swift
//  WTSinaWeibo
//
//  Created by  Wellim on 16/7/4.
//  Copyright © 2016年  Wellim. All rights reserved.
//

import UIKit
// buttontag中间值
private let tagNum = 1000

// 表情键盘类型没记
enum WTEmoticonToolbarType:Int{
   // 默认
  case Default = 0
    // Emoji表情
  case Emoji = 1
    // 浪小花
  case Lxh = 2

}

class WTEmoticonToolbar: UIStackView {
    //记录当前的按钮的选中状态
    var currentButton: UIButton?
    
    // 声明一个闭包告知外界哪个按钮点击
    var toolBarClosure: ((type:WTEmoticonToolbarType)->())?
    

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    // MARK: - 供外界调用方法
    func setCurrentButtonss(tag: Int)  {
      // 通过tag获取button
        let button = viewWithTag(tag + tagNum) as! UIButton
        
      // 如果当前的buton等于我们选中的button直接返回
        if currentButton == button {
            
            return
        }
        /*
         01-  传入的button 变为选中状态
         02 - currentButton 变为正常状态
         03 - currentButton == 传入的button
         */
        button.selected = true
        
        currentButton?.selected = false
        
        currentButton = button
        
        
    }
    
    // MARK: - 设置视图
    private func setUpUI(){
     
        //  UIStackView 的属性或(方法)
        // 排列方式
        axis = .Horizontal
        // 填充方式(子控件大小相等 填充父控件)
        distribution = .FillEqually
        /**
         *  UIStackView 的两个属性(方法)
         */
        
        
        backgroundColor = RandomColor()
        
        let defaultBtn = addChlidBottons("left", titile: "默认",type: .Default)
        let emojiBtn = addChlidBottons("mid", titile: "Emoji",type: .Emoji)
        let lxhBtn = addChlidBottons("right", titile: "浪小花",type: .Lxh)
        
//        // 添加约束
//        defaultBtn.snp_makeConstraints { (make) in
//          make.top.left.bottom.equalTo(self)
//          make.width.equalTo(emojiBtn)
//            
//        }
//        
//        emojiBtn.snp_makeConstraints { (make) in
//          make.top.bottom.equalTo(self)
//          make.left.equalTo(defaultBtn.snp_right)
//          make.width.equalTo(lxhBtn)
//            
//        }
//        
//        lxhBtn.snp_makeConstraints { (make) in
//          
//           make.top.right.bottom.equalTo(self)
//           make.left.equalTo(emojiBtn.snp_right)
//        }
        
        // 首次默认按钮为选中状态
        defaultBtn.selected = true
        currentButton = defaultBtn

    
    }
    
    // 创建button的公共方法
    // compose_emotion_table_left_normal
    //compose_emotion_table_left_selected
    
    func addChlidBottons(iamgeName:String,titile:String,type:WTEmoticonToolbarType) -> UIButton {
        
        let button = UIButton()
        //赋值枚举
        button.tag = type.rawValue + tagNum
        button.setBackgroundImage(UIImage(named: "compose_emotion_table_\(iamgeName)_normal"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "compose_emotion_table_\(iamgeName)_selected"), forState: UIControlState.Selected)
        button.setTitle(titile, forState: UIControlState.Normal)
        button.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        button.setTitleColor(UIColor.grayColor(), forState: UIControlState.Selected)
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        // 添加点击事件
        button.addTarget(self, action: "buttonClick:", forControlEvents: UIControlEvents.TouchUpInside)
      //   addSubview(button)
      
        /**
         *  UIStackView 的两个属性(方法) 添加子控件不同
         */
         addArrangedSubview(button)
        
        return button
    }
    
    // MARK: - 按钮监听方法
    @objc private func buttonClick(button:UIButton){
        if currentButton == button {
            
            return
        }
        
        /*
         01-  传入的button 变为选中状态
         02 - currentButton 变为正常状态
         03 - currentButton == 传入的button
         */
        button.selected = true
        
        currentButton?.selected = false
        
        currentButton = button
        
        
        // 执行闭包
        toolBarClosure?(type:WTEmoticonToolbarType(rawValue: button.tag - tagNum)!)
    
    
    }
    
    
}
